package com.example.selfcontrol.service.Admin.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.selfcontrol.config.Response;
import com.example.selfcontrol.entity.Article;
import com.example.selfcontrol.mapper.ArticleMapper;
import com.example.selfcontrol.service.Admin.AdminArticleJudge;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminArticleJudgeImpl implements AdminArticleJudge {
    @Autowired
    private ArticleMapper articleMapper ;

    @Override
    public List<Article> UnJudgeArticles(int pageNum, int pageSize) {
        Page<Article> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Article> wrapper = new QueryWrapper<>() ;
        wrapper.eq("active",0) ;
        IPage<Article> iPage = articleMapper.selectPage(page,wrapper) ;

        return iPage.getRecords() ;
    }

    @Override
    public int CountArticles() {
        QueryWrapper<Article> wrapper = new QueryWrapper<>() ;
        wrapper.eq("active",0) ;
        return articleMapper.selectCount(wrapper) ;
    }

    @Override
    public Response AllowArticle(int articleId, int judge) {
        Article article = articleMapper.selectById(articleId) ;

        if(judge == 1)  article.setActive(1);
        else article.setActive(-1);

        articleMapper.updateById(article) ;
        return Response.ok() ;
    }
}
